package com.sinaapp.gavinzhang.second;

import org.junit.Test;

/**
 * 利用矩阵的性质 对字符串进行对调
 * abc=>cba
 * Created by gavin on 15-8-7.
 */
public class Change {
    @Test
    public void ChangeStr()
    {
        char[] str = {'1','1','1','4','5','6','3','3','3','3'};

        int astart = 0;
        int astop = 2;

        int bstart = 6;
        int bstop = str.length-1;


        //a转置
        change(str,astart,astop);

        //b转置
        change(str,bstart,bstop);

        //不用交换的转置
        change(str,astop+1,bstart-1);

        //总转置
        change(str,0,str.length-1);

        System.out.print(new String(str));

    }

    public void change(char[] tmp,int a,int b)
    {
        if (a<0 || b>=tmp.length)return;
        while (a < b)
        {
            tmp[a] ^=tmp[b];
            tmp[b] ^=tmp[a];
            tmp[a] ^=tmp[b];
            ++a;
            --b;
        }
    }
}
